//
//  UIScrollView+Refresh.m
//

#import <MJRefresh/MJRefresh.h>
#import "UIScrollView+Refresh.h"

@implementation UIScrollView (Refresh)

- (BOOL)hasHeaderRefresh
{
    return (BOOL)self.mj_header;
}

- (BOOL)hasFooterRefresh
{
    return (BOOL)self.mj_footer;
}

- (void)addHeaderRefresh:(void (^)(void))complete
{
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^ {
        if (complete) {
            complete();
        }
    }];
}

- (void)addFooterRefresh:(void (^)(void))complete
{
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^ {
        if (complete) {
            complete();
        }
    }];
    [self hiddenFooter:YES];
}

- (void)addHeaderWithRefreshingTarget:(id)target refreshingAction:(SEL)action
{
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:target refreshingAction:action];
}

- (void)addFooterWithRefreshingTarget:(id)target refreshingAction:(SEL)action
{
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:target refreshingAction:action];
}

- (void)beginHeaderRefreshing
{
    [self.mj_header beginRefreshing];
}

- (void)beginFooterRefreshing
{
    [self.mj_footer beginRefreshing];
}

- (void)endHeaderRefreshing
{
    [self.mj_header endRefreshing];
}

- (void)endFooterRefreshing
{
    [self.mj_footer endRefreshing];
}

- (void)endFooterRefreshingNoMoreData
{
    [self.mj_footer endRefreshingWithNoMoreData];
}

- (void)hiddenHeader:(BOOL)hidden
{
    [self.mj_header setHidden:hidden];
}

- (void)hiddenFooter:(BOOL)hidden
{
    [self.mj_footer setHidden:hidden];
}

- (BOOL)isHeaderRefreshing
{
    return [self.mj_header isRefreshing];
}

- (BOOL)isFooterRefreshing
{
    return [self.mj_footer isRefreshing];
}

- (void)removeHeaderRefreshing
{
    if (self.mj_header.isRefreshing) {
        __weak typeof(self) OC = self;
        [self.mj_header endRefreshingWithCompletionBlock:^{
            OC.mj_footer = nil;
        }];
    }else {
        self.mj_header = nil;
    }
    
}

- (void)removeFooterRefreshing
{
    if (self.mj_footer.isRefreshing) {
        __weak typeof(self) OC = self;
        [self.mj_footer endRefreshingWithCompletionBlock:^{
            OC.mj_footer = nil;
        }];
    }else {
        self.mj_footer = nil;
    }
}

@end
